using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.CMS.Process;
using TjZheng.CMS.Process.Business;
using TjZheng.CMS.Process.DataEntity;
using TjZheng.Controls;
using TjZheng.Framework;
using TjZheng.Power.Business;
using TjZheng.Power.DataEntity;
using TjZheng.Power.RedisEntity;
using TjZheng.Redis;

namespace TjZheng.CMS.Controllers
{
    public class GroupInfoController : CMSController
    {
        /// <summary>
        /// 业务逻辑层
        /// </summary>
        BizGroupInfo objBizGroupInfo { get; set; }
        public GroupInfoController()
        {
            objBizGroupInfo = new BizGroupInfo();
        }
        /// <summary>
        /// 列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 列表数据或搜索数据
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">页条数</param>
        /// <returns></returns>
        [QueryMsSqlFilter]
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ListJson(int page, int rows)
        {
            return Json(objBizGroupInfo.GetPageResult(page - 1, rows, QueryResult.Condition, QueryResult.Parameters, "Id"));
        }
        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id">数据id</param>
        /// <returns></returns>
        public ActionResult EditPage(long? id)
        {
            GroupInfo objGroupInfo = null;
            if (id.HasValue)
            {
                objGroupInfo = objBizGroupInfo.GetRecord(id.Value);
            }
            else
            {
                objGroupInfo = new GroupInfo();
            }
            return View(objGroupInfo);
        }
        /// <summary>
        /// 编辑页面-保存
        /// </summary>
        /// <param name="objProvide">数据源</param>
        /// <returns></returns>
        //[ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Save(GroupInfo objProvide)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objProvide == null)
            {
                objInvokeResult.ResultCode = "ProvideNull";
                objInvokeResult.ResultMessage = "保存失败";
                return Json(objInvokeResult, "text/plain");
            }
            GroupInfo objTarget = null;
            if (objProvide.Id <= 0)
            {
                objTarget = new GroupInfo();
                Save(objTarget, objProvide);
                objTarget.GroupCode = Guid.NewGuid().ToString();
                objTarget.CreateDate = DateTime.Now;
                objTarget.Id = objBizGroupInfo.Add(objTarget);
            }
            else
            {
                objTarget = objBizGroupInfo.GetRecord(objProvide.Id);
                if (objTarget == null)
                {
                    objInvokeResult.ResultCode = "DataNull";
                    objInvokeResult.ResultMessage = "保存失败";
                    return Json(objInvokeResult, "text/plain");
                }
                Save(objTarget, objProvide);
                objBizGroupInfo.Update(objTarget);
            }
            if (IsCheckUserPowerRedis)
            {
                objBizGroupInfo.Store(objTarget, PowerVariable.ProviderName, PowerVariable.Db);
            }
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "保存成功";
            return Json(objInvokeResult, "text/plain");
        }
        /// <summary>
        /// 列表页面-删除
        /// </summary>
        /// <param name="id">数据id或id集合</param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (id.IsNull())
            {
                objInvokeResult.ResultCode = "IDNull";
                objInvokeResult.ResultMessage = "请先选择一条数据";
                return Json(objInvokeResult);
            }
            UserProcess objUserProcess = new UserProcess();
            objInvokeResult = objUserProcess.DeleteGroups(id.Split(',').Select(c => Convert.ToInt64(c)).ToList());
            return Json(objInvokeResult, "text/plain");
        }
        /// <summary>
        /// 用户组权限编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult EditGroupPowerInfo()
        {
            return View();
        }
        /// <summary>
        /// 用户组权限树，并设置选中
        /// </summary>
        /// <param name="id"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult CheckedGroupPowerInfoTreesJson(long id, long groupId)
        {
            UserProcess objUserProcess = new UserProcess();
            return Json(objUserProcess.BindCheckedPowerInfoTrees(id, groupId, true, UserPowerType.GroupPower));
        }
        /// <summary>
        /// 用户组权限-保存
        /// </summary>
        /// <returns></returns>
        public ActionResult SaveGroupPower(List<GroupPowerInfo> objGroupPowerInfoList, long groupId)
        {
            UserProcess objUserProcess = new UserProcess();
            return Json(objUserProcess.SaveGroupPower(objGroupPowerInfoList, groupId), "text/plain");
        }
        /// <summary>
        /// 用户组权限查看
        /// </summary>
        /// <returns></returns>
        public ActionResult ViewGroupPowerInfo()
        {
            return View();
        }
        /// <summary>
        /// 用户组权限树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult GroupPowerInfoTreesJson(long id, long groupId)
        {
            //UserProcess objUserProcess = new UserProcess();
            //return Json(objUserProcess.BindPowerInfoTrees(id, groupId, true, UserPowerType.GroupPower));
            if (IsCheckUserPowerRedis)
            {
                UserRedisProcess objUserRedisProcess = new UserRedisProcess();
                return Json(objUserRedisProcess.BindPowerInfoTrees(id, groupId, true, UserPowerType.GroupPower));

            }
            else
            {
                UserProcess objUserProcess = new UserProcess();
                return Json(objUserProcess.BindPowerInfoTrees(id, groupId, true, UserPowerType.GroupPower));
            }
        }
        /// <summary>
        /// 用户组继承权限编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult EditUserPowerInfo()
        {
            return View();
        }
        /// <summary>
        /// 继承用户组部分权限树，并设置选中
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult CheckedGroupUserPowerInfoTreesJson(long id, long userId)
        {
            UserProcess objUserProcess = new UserProcess();
            return Json(objUserProcess.BindCheckedGroupUserPowerInfoTrees(id, userId, true));
        }
        /// <summary>
        /// 用户组继承权限-保存
        /// </summary>
        /// <param name="objUserPowerInfoList"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ActionResult SaveUserPower(List<UserPowerInfo> objUserPowerInfoList, long userId)
        {
            UserProcess objUserProcess = new UserProcess();
            return Json(objUserProcess.SaveUserPower(objUserPowerInfoList, userId), "text/plain");
        }
        /// <summary>
        /// 用户独立权限编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult EditUserDependentPowerInfo()
        {
            return View();
        }
        /// <summary>
        /// 用户独立权限树，并设置选中
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult CheckedUserDependentPowerInfoTreesJson(long id, long userId)
        {
            UserProcess objUserProcess = new UserProcess();
            return Json(objUserProcess.BindCheckedPowerInfoTrees(id, userId, true, UserPowerType.DependentUserPower));
        }
        /// <summary>
        /// 用户独立权限-保存
        /// </summary>
        /// <param name="objUserPowerInfoList"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ActionResult SaveUserDependentPower(List<UserPowerInfo> objUserPowerInfoList, long userId)
        {
            UserProcess objUserProcess = new UserProcess();
            return Json(objUserProcess.SaveUserDependentPower(objUserPowerInfoList, userId), "text/plain");
        }
        /// <summary>
        /// 用户权限查看
        /// </summary>
        /// <returns></returns>
        public ActionResult ViewUserPowerInfo()
        {
            return View();
        }
        /// <summary>
        /// 用户权限树
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult UserPowerInfoTreesJson(long id, long userId)
        {
            UserProcess objUserProcess = new UserProcess();
            var objUserPowerType = objUserProcess.GetUserPowerType(userId);
            if (!objUserPowerType.HasValue)
            {
                return Content("对不起，用户不存在或已删除");
            }
            //return Json(objUserProcess.BindPowerInfoTrees(id, userId, true, objUserPowerType.Value));

            if (IsCheckUserPowerRedis)
            {
                UserRedisProcess objUserRedisProcess = new UserRedisProcess();
                return Json(objUserRedisProcess.BindPowerInfoTrees(id, userId, true, objUserPowerType.Value));
            }
            else
            {
                return Json(objUserProcess.BindPowerInfoTrees(id, userId, true, objUserPowerType.Value));
            }
        }
        /// <summary>
        /// 除根节点用户权限列表树
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult UserPowerInfoTreeListJson(long id)
        {
            UserProcess objUserProcess = new UserProcess();
            var objUserPowerType = objUserProcess.ConvertoUserPowerType(CurrentUser.PowerType);
            if (!objUserPowerType.HasValue)
            {
                return Content("对不起，访问权限类型异常");
            }
            //return Json(objUserProcess.BindPowerInfoTreeListWithoutUnDisplay(id, CurrentUser.Id, false, objUserPowerType.Value));

            if (IsCheckUserPowerRedis)
            {
                UserRedisProcess objUserRedisProcess = new UserRedisProcess();
                return Json(objUserRedisProcess.BindPowerInfoTreeListWithoutUnDisplay(id, CurrentUser.Id, false, objUserPowerType.Value));
            }
            else
            {
                return Json(objUserProcess.BindPowerInfoTreeListWithoutUnDisplay(id, CurrentUser.Id, false, objUserPowerType.Value));
            }
        }
        /// <summary>
        /// 除父节点和按钮的用户子权限列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult UserPowerInfoChildrenWithoutButton(long id)
        {
            UserProcess objUserProcess = new UserProcess();
            var objUserPowerType = objUserProcess.ConvertoUserPowerType(CurrentUser.PowerType);
            if (!objUserPowerType.HasValue)
            {
                return Content("对不起，访问权限类型异常");
            }
            //return Json(objUserProcess.BindPowerInfoChildrenWithoutButton(id, CurrentUser.Id, false, objUserPowerType.Value));

            if (IsCheckUserPowerRedis)
            {
                UserRedisProcess objUserRedisProcess = new UserRedisProcess();
                return Json(objUserRedisProcess.BindPowerInfoChildrenWithoutButton(id, CurrentUser.Id, false, objUserPowerType.Value));
            }
            else
            {
                return Json(objUserProcess.BindPowerInfoChildrenWithoutButton(id, CurrentUser.Id, false, objUserPowerType.Value));
            }
        }
        /// <summary>
        /// 同步Redis缓存
        /// </summary>
        /// <param name="id">用户组Id</param>
        /// <returns></returns>
        public ActionResult SyncRedis(int id)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (IsCheckUserPowerRedis)
            {
                if (id <= 0)
                {
                    objInvokeResult.ResultCode = "ParameterError";
                    objInvokeResult.ResultMessage = "参数错误";
                    return Json(objInvokeResult, "text/plain");
                }
                GroupInfo objGroupInfo = objBizGroupInfo.GetRecord(id);
                if (objGroupInfo == null)
                {
                    objInvokeResult.ResultCode = "DataNull";
                    objInvokeResult.ResultMessage = "没有相关的数据同步";
                    return Json(objInvokeResult, "text/plain");
                }
                objBizGroupInfo.Store(objGroupInfo, PowerVariable.ProviderName, PowerVariable.Db);
                BizGroupPowerInfo objBizGroupPowerInfo = new BizGroupPowerInfo();
                List<GroupPowerInfo> objGroupPowerInfoList = objBizGroupPowerInfo.GetList("GroupId=" + id);
                if (objGroupPowerInfoList.IsNull())
                {
                    objInvokeResult.ResultCode = "DataNull";
                    objInvokeResult.ResultMessage = "没有相关的数据同步";
                    return Json(objInvokeResult, "text/plain");
                }
                GroupPower objGroupPower = new GroupPower
                {
                    GroupId = id,
                    PowerIds = objGroupPowerInfoList.Select(c => c.PowerId).ToList()
                };
                RedisHelper.Store<GroupPower>(objGroupPower, PowerVariable.ProviderName, PowerVariable.Db);
            }
            objInvokeResult.ResultMessage = "同步成功";
            return Json(objInvokeResult, "text/plain");
        }
        /// <summary>
        /// 数据赋值
        /// </summary>
        /// <param name="objTarget">目标数据</param>
        /// <param name="objProvide">数据源</param>
        void Save(GroupInfo objTarget, GroupInfo objProvide)
        {
            objTarget.GroupName = objProvide.GroupName.CutWord(64);
            objTarget.Description = objProvide.Description.CutWord(256);
        }
    }
}
